<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title> BrowserReptile View </title>
    <!-- 依 赖 样 式 -->
    <link rel="stylesheet" href="./module/pear/css/pear.css"/>
</head>
<body class="pear-container">
<div id="contents"></div>
</body>

<!-- 依 赖 脚 本 -->
<script src="./module/layui/layui.js"></script>
<script src="./module/pear/pear.js"></script>
<script>
    var contentWin = undefined

    window.onload = function () {
        querySuccess()
    }

    var jsConnector = {
        API_PREFIX: "",
        HTML_PATH: "",
        init: function () {
            layui.use(['layer', 'jquery', 'tab', 'form', 'tool', 'request'], function () {
                layui.tab.render({
                    elem: 'contents',
                    roll: false,
                    tool: false,
                    width: '100%',
                    height: '655px',
                    index: 0,
                    session: false,
                    tabMax: 30,
                    data: [{
                        id: "1",
                        title: "首页",
                        url: jsConnector.HTML_PATH + "/content.html",
                        close: false
                    }],
                });
            });
        },
        websocket: function () {
            const ws = new WebSocket("ws://127.0.0.1:48603/content"); //创建WebSocket连接
            ws.onopen = function () {
                alert(JSON.stringify({
                    type: 'socketSuccess'
                }))
                layui.tool.toast.success('labmam Server', '连接成功');
            }
            ws.onclose = function (e) {
                layui.tool.toast.error('labmam Server', '断开连接！');
                jsConnector.websocket()
            }
            ws.onerror = function (e) {
                layui.tool.toast.error('labmam Server', '连接错误：' + e);
            }
            ws.onmessage = function (e) {
                console.log("ws获取到：" + e.data)
                var data = JSON.parse(e.data)
                switch (data['method']) {
                    case 'log':
                        contentWin.addLog(data['data'])
                        break
                    case 'refresh':
                        location.reload()
                        break
                    case 'getTaskList':
                        contentWin.getTaskList(data['data'])
                        break
                    case 'toast':
                        layui.tool.toast.success(data['toast'], data['msg']);
                        break
                    case 'save_back':
                        save_back(data['data'])
                        break
                    case 'getTask_back':
                        getTask_back(data['data'])
                        break
                    case 'deploy_file_back':
                        deploy_file_back(data['data'])
                        break
                    case 'saveDeploy_back':
                        saveDeploy_back(data['data'])
                        break
                    case 'runTask_back':
                        contentWin.doRunning(data['data'])
                        break
                    case 'shutTask_back':
                        contentWin.shut(data['data'])
                        break
                    case 'taskAllDone':
                        contentWin.done()
                        break
                    case 'taskProgress':
                        contentWin.refRunnerProgress(data['data'])
                        break
                }
            }
        }
    };

    //判断程序加载是否成功
    function querySuccess(i) {
        if (i >= 75) {
            alert(JSON.stringify({
                type: 'function',
                method: 'initError'
            }))
            return
        }
        setTimeout(() => {
            if (contentWin !== undefined) {
                alert(JSON.stringify({
                    type: 'initSuccess'
                }))
            } else
                querySuccess(i++)
        }, 200)
    }

    function closeCurrentTab(_back) {
        layui.tab.delCurrentTabByElem('contents', function (id) {
            contentWin.reload()
            if (typeof _back === 'function') {
                _back(window)
            }
        });
    }

    function save_back(res) {
        if (res.code === 0) {
            parent.window.closeCurrentTab()
            layui.tool.toast.success('提示', res['message']);
        } else {
            layui.tool.toast.error('提示', res['message']);
        }
        layui.tool.loading.remove(500)
    }

    function getTask_back(res) {
        if (res.code === 0) {
            let data = res.data
            contentWin.cacheTab['task' + data.index].showData(data['data'])
        } else {
            parent.window.closeCurrentTab()
            layui.tool.toast.error('提示', res['message']);
        }
        layui.tool.loading.remove(600)
    }

    function deploy_file_back(res) {
        if (res.code === 0) {
            let data = res.data
            if (contentWin.deployDom != null) {
                contentWin.deployDom.find('[name=' + data.name + ']').val(data.filePath)
            }
        } else
            layui.tool.toast.error('提示', "文件获取失败");
    }

    function saveDeploy_back(res) {
        contentWin.layui.tool.loading.remove(500)
        if (res.code === 0) {
            layui.tool.toast.success('提示', "配置完成");
        } else
            layui.tool.toast.error('提示', res['message']);
    }

    function getJsConnector() {
        return jsConnector;
    }

</script>
</html>